<?php
class HinhanhsController extends AppController{
    var $name = 'Hinhanhs';

    //File Upload Settings
    function loadconfig(){
        $cfupload = array(
                        "upload_url" => "doupload",
                        "file_size_limit" => "102400",
                        "file_types" => "*.jpg;*.png",
                        "file_types_description" => "Images Files",
                        "file_upload_limit" => "100",
                        "file_queue_limit" => "0",
                        "button_image_url" => "/share/img/btn_upload.png",
                    );
        return $cfupload;
    }
    function index(){
        $mess = "Xin chao! ";
        echo $mess;
        $this->layout = FALSE;
        $this->render(FALSE);
    }
    function show($str="default"){
        echo $str;
        $this->layout = FALSE;
        $this->render(FALSE);
    }
    function upload(){
        $cfupload = $this->loadconfig();
        $this->set($cfupload);
        $this->layout = FALSE;
    }
    function doupload(){
        if (isset($_POST["PHPSESSID"])) {
            session_id($_POST["PHPSESSID"]);
        }
        session_start();
        $_SESSION['count_file'] +=1;
        $data = $_SESSION['count_file'];

        if($data>5){
            header("HTTP/1.1 511 File qua nhieu");
            exit(0);
        }

        // The Demos don't save files


        // In this demo we trigger the uploadError event in SWFUpload by returning a status code other than 200 (which is the default returned by PHP)
        if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
            // Usually we'll only get an invalid upload if our PHP.INI upload sizes are smaller than the size of the file we allowed
            // to be uploaded.
            header("HTTP/1.1 500 File Upload Error");
            if (isset($_FILES["Filedata"])) {
                echo $_FILES["Filedata"]["error"];
            }
            exit(0);
        }else{
            $valid_chars_regex = '.A-Z0-9_ !@#$%^&()+={}\[\]\',~`-';
            $save_path = str_replace("\\", "/", getcwd()) . "imgupload/";
            $save_path = str_replace("share/app/webroot","",$save_path);
            //chdir ($save_path);
            $file_name = preg_replace('/[^'.$valid_chars_regex.']|\.+$/i', "", basename($_FILES["Filedata"]['name']));
            $linkfile =$save_path.$file_name;
            move_uploaded_file($_FILES["Filedata"]["tmp_name"], $linkfile);
            echo $linkfile;

            $data = $linkfile;
            $fp = fopen('data.txt', 'a');
            fwrite($fp, $data);
            fclose($fp);
        }
        $this->layout = null;
        $this->render(FALSE);

    }
}
?>